//
//  XSDrawerViewController.m
//  绿地管理系统
//
//  Created by rimi on 16/1/18.
//  Copyright © 2016年 hexs. All rights reserved.
//

#import "XSDrawerViewController.h"
#import "XSViewController.h"

enum{
    ButtonTag = 100,
    ImageTag = 200,
    Tag = 500,
};

@interface XSDrawerViewController ()

// tabBar 图片
@property (nonatomic, strong) NSArray   *tabBarImageArray;
@property (nonatomic, strong) NSArray   *viewControllers;
@property (nonatomic, assign) NSInteger selectedIndex;
@property (nonatomic, strong) UIButton  *seletedButton;

@property (nonatomic, strong) UIView            *tabBarView;


@end

@implementation XSDrawerViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    
    _tabBarImageArray = @[@"我的人脉未点击_03@2x",@"人脉库未点击_03@2x",@"可能未点击_05@2x",@"添加关系未点击_03@2x",@"关于未点击_03@2x",@"我的人脉点击_03@2x",@"人脉库点击_03@2x",@"可能点击_05@2x",@"添加关系点击_03@2x",@"关于点击_03@2x"];
    NSMutableArray *viewControllers = [NSMutableArray array];
    NSArray *names = @[@"我的人脉_03",@"人脉库_03",@"可能认识的人_03",@"人脉关系建立_03@2x",@"关于title@2x"];
    NSArray *controllerNames = @[@"MyContactsViewController",@"ContactsLibraryViewController",@"MayViewController",@"AddContactsViewController",@"AboutViewController"];
    NSInteger index = 0;
    for (NSString *name in controllerNames) {
        UIViewController *controller = [[NSClassFromString(name) alloc] init];
//        controller.title = names[index];
        UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:controller];
        [navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:21]}];
        [navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"顶"] forBarMetrics:UIBarMetricsDefault];
        
        
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:names[index]]];
        imageView.frame = CGRectMake(0, 0, 80, 20);
        imageView.center = navigationController.navigationBar.center;
        imageView.contentMode = UIViewContentModeCenter;
        [navigationController.navigationBar addSubview:imageView];
//        navigationController.navigationItem.titleView = imageView;

        
//        navigationController.navigationBar.userInteractionEnabled = YES;
        [viewControllers addObject:navigationController];
        index++;
    }
    self.viewControllers = viewControllers;
    
    
    [self addChildViewController:self.viewControllers[0]];
    [self.view insertSubview:((UIViewController *)self.viewControllers[0]).view atIndex:0];
    [self.viewControllers[0] didMoveToParentViewController:self];
    
}

- (void)initializeAppearance
{
    [self.view addSubview:self.tabBarView];
    // Button
    for (int i = 0; i < 5; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, 0, (375-10)/5, 50);
        button.tag = ButtonTag + i;
        [button addTarget:self action:@selector(onButton:) forControlEvents:UIControlEventTouchUpInside];
        button.center = CGPointMake((375/5/2) + i*(375/5), self.tabBarView.frame.origin.y + 25);
        [button setImage:[UIImage imageNamed:_tabBarImageArray[i]] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:_tabBarImageArray[i+5]] forState:UIControlStateSelected];
        [self.view addSubview:button];
//        [self.tabBarView addSubview:button];
    }
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//    XSViewController *drawerVC = (XSViewController *)self.parentViewController;
//    [drawerVC openLeftDrawer];
}

#pragma mark -- 交互事件
- (void)onButton:(UIButton *)sender
{
    NSInteger index = sender.tag - ButtonTag;
    self.selectedIndex = index;
    
    _seletedButton.selected = NO;
    sender.selected = YES;
    self.seletedButton = sender;
    

}

- (void)setSelectedIndex:(NSInteger)selectedIndex
{
    UIViewController *currentViewController = self.viewControllers[_selectedIndex];
    [currentViewController.view removeFromSuperview];
    [currentViewController removeFromParentViewController];
    [currentViewController willMoveToParentViewController:nil];
    
    _selectedIndex = selectedIndex;
    
    UIViewController *selectedViewController = self.viewControllers[_selectedIndex];
//    selectedViewController.view.frame = self.view.bounds;
    [self addChildViewController:selectedViewController];
    [self.view insertSubview:selectedViewController.view atIndex:0];
    [selectedViewController didMoveToParentViewController:self];
    
}

- (void)setViewControllers:(NSArray *)viewControllers
{
    _viewControllers = viewControllers;
    [self initializeAppearance];
}

#pragma mark -- getter
- (UIView *)tabBarView
{
    if (_tabBarView == nil) {
        _tabBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 667 - 50, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds))];
//        _tabBarView.backgroundColor = [UIColor colorWithRed:25/255.0 green:25/255.0 blue:25/255.0 alpha:1.0];
        [_tabBarView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"底部_02@2x"]]];
    }
    return _tabBarView;
}
- (UIButton *)seletedButton
{
    if (_seletedButton == nil) {
        _seletedButton = [UIButton buttonWithType:UIButtonTypeSystem];
        _seletedButton.frame = CGRectMake(0, 0, (375-10)/5, 50);
    }
    return _seletedButton;
}

@end













